/**
 * 
 */
package mx.netbrain.bean;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import mx.netbrain.dao.PropertiesSignatureDao;
import mx.netbrain.dao.SignatureDao;
import mx.netbrain.dao.impl.PropertiesSignatureDaoImpl;
import mx.netbrain.dao.impl.SignatureDaoImpl;
import mx.netbrain.model.Signature;

/**
 * @author julian
 * 
 */
public class SignatureBean {

	private Signature signature;
	private Boolean active;

	public SignatureBean() {
		SignatureDao signatureDao = new SignatureDaoImpl();
		signature = signatureDao.findById();
		active = signature.isActiva();
		System.out.println(active);
	}

	public void addMessage() {
		String summary = active ? "Firma electrónica activada"
				: "Firma electrónica desactivada";
		FacesContext.getCurrentInstance().addMessage(null,
				new FacesMessage(summary));
		if (active) {
			SignatureDao signatureDao = new SignatureDaoImpl();
			signature = signatureDao.findById();
			signature.setActiva(active);
			signatureDao.update(signature);
			PropertiesSignatureDao propertiesSignatureDao = new PropertiesSignatureDaoImpl();
			propertiesSignatureDao.updateStatus(1);
		} else {
			SignatureDao signatureDao = new SignatureDaoImpl();
			signature = signatureDao.findById();
			signature.setActiva(active);
			signatureDao.update(signature);
			PropertiesSignatureDao propertiesSignatureDao = new PropertiesSignatureDaoImpl();
			propertiesSignatureDao.updateStatus(2);
		}
	}

	public Signature getSignature() {
		if (signature == null) {
			signature = new Signature();
		}
		return signature;
	}

	public void setSignature(Signature signature) {
		this.signature = signature;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

}
